using System.Collections.Generic;
using System.Windows.Forms;
using ClientApi;
using CommonGui.Progress;
using DataModel.Reports;

namespace RitaReports.WorkflowResponse
{
    public class WorkflowResponseReport : IRitaReport
    {
        #region Fields

        private WorkflowResponseReportInitData _initData;

        private readonly WorkflowResponseFilter _filter = new WorkflowResponseFilter();
        private readonly WorkflowResponseViewer _viewer = new WorkflowResponseViewer();
        private WorkflowResponseDataSource _dataSource;

        #endregion

        public WorkflowResponseReport()
        {
            _filter.FilterConditiondsChanged += _filter_FilterConditiondsChanged;
        }

        void _filter_FilterConditiondsChanged(object sender, System.EventArgs e)
        {
            if (_dataSource != null)
            {
                _dataSource.ApplyLocalFilter(_filter.FilterConditionds);
                _viewer.UpdateReportViewer(_dataSource);
            }
        }

        #region IRitaReport Members

        public string Name
        {
            get { return "Workflow Response"; }
        }

        public IReportMap ReportMap
        {
            get { return null; }
        }

        public IReportFilter Filter
        {
            get { return _filter; }
        }

        public IReportViewer Viewer
        {
            get { return _viewer; }
        }

        public IReportDataSource DataSource
        {
            get { return _dataSource; }
        }

        public IRitaReport[] SubReports
        {
            get { return null; }
        }

        public void UpdateDataSource(IReportFilterConditionds reportFilter, IWin32Window owner)
        {
            List<JobProgressItem> jobs = new List<JobProgressItem>();
            WorkflowResponseFilterConditionds filter = (WorkflowResponseFilterConditionds)reportFilter;
            JobProgressItem job;

            if (_initData == null)
            {
                job = new JobProgressItem("Downloading report...");
                job.ProgressInformator = ReportDataProvider.Instance;
                job.Job = delegate
                              {
                                  _initData = ReportDataProvider.Instance.GetWorkflowResponseReportInitData();
                              };
                jobs.Add(job);
            }

            job = new JobProgressItem("Downloading report data...");
            job.ProgressInformator = ReportDataProvider.Instance;
            job.Job = delegate
                          {
                              _dataSource = new WorkflowResponseDataSource(
                                  ReportDataProvider.Instance.GetWorkflowResponseReportData(filter.OrgId));
                          };

            jobs.Add(job);

            JobsProgressForm.ShowProgress(jobs, owner);

            if (_initData != null && _filter.Inited == false)
            {
                _filter.Init(_initData);
            }

            _dataSource.ApplyLocalFilter(_filter.FilterConditionds);
        }

        #endregion
    }
}
